<template>
  <div class="fans-list">
    <div
      class="fans-item"
      v-for="item in fansList"
      :key="item.userId"
      @click="locateToUserProfile(item.userId)"
      v-if="fansList.length > 0"
    >
      <div class="fans-item-avatar">
        <img
          :src="item.avatarUrl"
          alt=""
        />
      </div>
      <div class="fans-item-info">
        <div class="fans-item-info-name">{{ item.nickname }}</div>
        <div class="fans-item-info-signature">{{ item.signature }}</div>
      </div>
      <div
        class="fans-item-info-action"
        v-if="item.userId != userStore.getUserId"
      >
        <a-button
          type="primary"
          @click.stop="handleSubscribe(item)"
        >
          {{ item.isFollow ? '已关注' : '关注' }}
        </a-button>
      </div>
    </div>

    <Pagination
      v-if="fansList.length > 0"
      class="pagination-wrap"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getFansData"
    />

    <div v-else>
      <a-empty />
    </div>
  </div>
</template>

<script setup lang="ts">
import type { CvSubscribePageReqVO } from '@/types/subscribe'
import { cancelSubscribeByUserId, getFansPage, saveSubscribe } from '@/api/subscribe'
import { CvSubscribePageRespVO, CvSubscribeSaveReqVO } from '@/types/subscribe'
import { getAccessToken } from '@/utils/auth'
import { message } from 'ant-design-vue'
import { useUserStore } from '@/store/modules/user'

const userStore = useUserStore()

const profileUserId = defineModel()
const fansList = ref<CvSubscribePageRespVO[]>([])
const total = ref(0)

const queryParams = ref<CvSubscribePageReqVO>({
  userId: null,
  pageNo: 1,
  pageSize: 10
})

onMounted(async () => {
  queryParams.value.userId = profileUserId.value
  await getFansData()
})

/**
 * 获取粉丝数据
 */
const getFansData = async () => {
  const res = await getFansPage(queryParams.value)
  fansList.value = res.list
  total.value = res.total
}

/**
 * 关注/取消关注
 */
const handleSubscribe = async (item: any) => {
  if (!getAccessToken()) {
    message.error('请登录')
    return
  }

  if (item.isFollow) {
    await cancelSubscribeByUserId(item.userId)
    item.isFollow = false
    message.success('取关成功')
  } else {
    await saveSubscribe(<CvSubscribeSaveReqVO>{
      targetUserId: item.userId
    })
    item.isFollow = true
    message.success('关注成功')
  }
}

/**
 * 跳转到用户个人主页
 */
const locateToUserProfile = (userId: number) => {
  window.open(`/profile?userId=${userId}`)
}
</script>

<style scoped>
.fans-list {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px 0;
}

.fans-item {
  display: flex;
  align-items: center;
  padding: 16px;
  margin-bottom: 16px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  cursor: pointer;
}

.fans-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.fans-item-avatar {
  width: 60px;
  height: 60px;
  margin-right: 20px;
  flex-shrink: 0;
}

.fans-item-avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.fans-item-info {
  flex: 1;
  min-width: 0;
  margin-right: 20px;
}

.fans-item-info-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fans-item-info-signature {
  font-size: 14px;
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.fans-item-info-action {
  flex-shrink: 0;
}

.pagination-wrap {
  margin-top: 20px;
  text-align: center;
}

@media (max-width: 768px) {
  .fans-list {
    padding: 10px;
  }

  .fans-item {
    padding: 12px;
    margin-bottom: 12px;
  }

  .fans-item-avatar {
    width: 50px;
    height: 50px;
    margin-right: 12px;
  }

  .fans-item-info-name {
    font-size: 14px;
  }

  .fans-item-info-signature {
    font-size: 13px;
  }
}
</style>
